<%@ page language="java"
	import="java.util.*,ll.sms.util.*,org.apache.commons.lang.*"
	pageEncoding="UTF-8"%>
<%@page import="ll.sms.web.ParamUtils"%>
<%@page import="ll.sms.web.WebBusinessImpl"%>
<%@page import="ll.sms.pojo.SmsSuggest"%>
<%@page import="ll.sms.pojo.SmsBlackList"%>
<%@page import="ll.sms.web.SmsAppContainer"%>
<%
	String currentUserId = SmsUtil.sessionHelper(session,
			AdminProperty.aid);
	if (StringUtils.isBlank(currentUserId)) {
		return;
	}
	//String currendUpwd = SmsUtil.sessionHelper(session,AdminProperty.apwd);
	String flag = request.getParameter("flag");

	//修改密码
	if ("changepassword".equals(flag)) {
		//String pwd = ParamUtils.getParameter(request,"pwd");
		String npwd = ParamUtils.getParameter(request, "npwd");
		String repwd = ParamUtils.getParameter(request, "renpwd");
		if (StringUtils.isBlank(npwd) || StringUtils.isBlank(repwd)) {
			out.print(ExtJsonBuilder.formFailure("密码必须填写完整"));
			return;
		}
		if (!npwd.equals(repwd)) {
			out.print(ExtJsonBuilder.formFailure("两次密码不一致"));
			return;
		}

		out.print(WebBusinessImpl.getInstance().updateAdminPassword(
				currentUserId, npwd));
		return;
	}

	//查询所有建议信息
	if ("suggest".equals(flag)) {
		List<SmsSuggest> list = WebBusinessImpl.getInstance()
				.findAllSuggest();
		out.print(ExtJsonBuilder.toExtJsonString(list == null ? 0
				: list.size(), list));
		return;
	}

	if ("suggestdel".equals(flag)) {
		String i = WebBusinessImpl.getInstance().deleteSuggest(
				ParamUtils.getParameter(request, "delid"));
		out.print(i);
		return;
	}

	if ("changechannel".equals(flag)) {
		String channelId = ParamUtils.getParameter(request, "uchannel");
		String i = WebBusinessImpl
				.getInstance()
				.changeDefaultChannelByProxyId(channelId, currentUserId);
		out.print(i);
		return;
	}

	//cacel proxy
	if ("cancleproxy".equals(flag)) {
		String pid = ParamUtils.getParameter(request, "pid");
		String statu = ParamUtils.getParameter(request, "statu");
		String acompany = ParamUtils.getParameter(request, "acompany");
		String auname = ParamUtils.getParameter(request, "auname");

		if (StringUtils.isBlank(pid)) {
			return;
		}

		//禁用
		if ("1".equals(statu)) {
			SmsBlackList b = new SmsBlackList();
			b.setBid(SmsUtil.getPrimaryKey());
			b.setBrealname(acompany);
			b.setBstatu(-1);
			b.setBtype(1);
			b.setBuserid(pid);
			b.setBusername(auname);
			String i = WebBusinessImpl.getInstance().enableProxy(b);
			out.print(i);
			return;
		}
		//取消禁用
		if ("-1".equals(statu)) {
			String i = WebBusinessImpl.getInstance().ableProxy(pid,
					SmsConstant.BLACK_PROXY);
			out.print(i);
			return;
		}

	}

	if ("changechannel".equals(flag)) {
		String channelId = ParamUtils.getParameter(request, "uchannel");
		String i = WebBusinessImpl
				.getInstance()
				.changeDefaultChannelByProxyId(channelId, currentUserId);
		out.print(i);
		return;
	}

	//cacel proxy
	if ("cancleuser".equals(flag)) {
		String pid = ParamUtils.getParameter(request, "pid");
		String statu = ParamUtils.getParameter(request, "statu");
		String acompany = ParamUtils.getParameter(request, "acompany");
		String auname = ParamUtils.getParameter(request, "auname");

		if (StringUtils.isBlank(pid)) {
			return;
		}

		//禁用
		if ("1".equals(statu)) {
			SmsBlackList b = new SmsBlackList();
			b.setBid(SmsUtil.getPrimaryKey());
			b.setBrealname(acompany);
			b.setBstatu(-1);
			b.setBtype(1);
			b.setBuserid(pid);
			b.setBusername(auname);
			String i = WebBusinessImpl.getInstance().enableProxy(b);
			out.print(i);
			return;
		}
		//取消禁用
		if ("-1".equals(statu)) {
			String i = WebBusinessImpl.getInstance().ableProxy(pid,
					SmsConstant.BLACK_USER);
			out.print(i);
			return;
		}

	}

	//删除代理
	if ("delproxy".equals(flag)) {
		//只有admin才能删除
		if (!"1".equals(SmsUtil.sessionHelper(session,
				AdminProperty.arank))) {
			return;
		}
		String delid = ParamUtils.getParameter(request, "delid");
		SmsAppContainer.initBlackList();
		
		out.print(WebBusinessImpl.getInstance().deleteProxy(delid));
		SmsAppContainer.initBlackList();
		return;
	}
	//删除用户
	if ("deluser".equals(flag)) {
		String delid = ParamUtils.getParameter(request, "delid");
		out.print(WebBusinessImpl.getInstance().deleteUser(delid));
		SmsAppContainer.initBlackList();
		return;
	}

	if ("deloperator".equals(flag)) {
		String delid = ParamUtils.getParameter(request, "delid");
		out.print(WebBusinessImpl.getInstance().deleteOperator(delid));
		return;
	}

	//检测用户
	if ("usernamecheck".equals(flag)) {
		String type = ParamUtils.getParameter(request, "type");
		String uname = ParamUtils.getParameter(request, "name");
		if ("a".equals(type)) {
			int i = WebBusinessImpl.getInstance().countTable("sms_admin"," where auname='"+uname+"' ");
			if(i==0){
			out.print(ExtJsonBuilder.formSuccess("没有重复"));
			}else{
			out.print(ExtJsonBuilder.formFailure("重复"));
			}
			return;
		}
		if ("u".equals(type)) {

		int i = WebBusinessImpl.getInstance().countTable("sms_user"," where uuname='"+uname+"'");
			if(i==0){
			out.print(ExtJsonBuilder.formSuccess("没有重复"));
			}else{
			out.print(ExtJsonBuilder.formFailure("重复"));
			}
			return;
		}
		return;
	}

	//delete message
	if("delmessages".equals(flag)){
		String delid = ParamUtils.getParameter(request, "delid");
		String returnStr = WebBusinessImpl.getInstance().deleteMessages(delid);
		out.print(returnStr);
		return;
		
	}
	
	//change max send count
	if("maxsend".equals(flag)){
	try{
	int num = ParamUtils.getIntParameter(request,"num",-1);
	SmsUtil.modifySmsProperties("MAX_SEND",num+"");
	SmsAppContainer.grobleContainer.put("MAX_SEND",num);
	out.print(ExtJsonBuilder.formSuccess("最大单次发送值设定为"+num));
	}catch(Exception e){
		e.printStackTrace();
		out.print(ExtJsonBuilder.formFailure("修改失败"));
	}
	return;
	}
%>

